using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.ModuleDataAuthorizeScheme;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 数据权限方案
    /// 版 本：V2.6.200509
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-11-08
    /// </summary>
    [Route("api/System/[controller]")]
    public class ModuleDataAuthorizeSchemeController : BaseApiController
    {
        private ModuleDataAuthorizeSchemeBll moduleDataAuthorizeSchemeBll = new ModuleDataAuthorizeSchemeBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="moduleId">功能主键</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpGet("{moduleId}/List")]
        public async Task<ResponseResult> GetList(string moduleId, KeywordInput input)
        {
            var data = await moduleDataAuthorizeSchemeBll.GetList(moduleId);
            if (!string.IsNullOrEmpty(input.keyword))
            {
                data = data.FindAll(t => t.EnCode.Contains(input.keyword) || t.FullName.Contains(input.keyword));
            }
            var listModuleDataAuthorizeScheme = data.MapToList<ModuleDataAuthorizeSchemeListOutput>();
            return Success(new { list = listModuleDataAuthorizeScheme });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = (await moduleDataAuthorizeSchemeBll.GetInfo(id)).MapTo<ModuleDataAuthorizeSchemeInfoOutput>();
            return Success(data);
        }
        #endregion

        #region POST
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await moduleDataAuthorizeSchemeBll.GetInfo(id);
            if (entity != null)
            {
                var isOk = await moduleDataAuthorizeSchemeBll.Delete(entity);
                return isOk > 0 ? Success("删除成功") : Error("删除失败");
            }
            else
            {
                return Success("删除失败");
            }
        }

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] ModuleDataAuthorizeSchemeCrInput input)
        {
            var entitys = input.MapTo<ModuleDataAuthorizeSchemeEntity>();
            var isOk = await moduleDataAuthorizeSchemeBll.Create(entitys);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] ModuleDataAuthorizeSchemeUpInput input)
        {
            var entitys = input.MapTo<ModuleDataAuthorizeSchemeEntity>();
            var isOk = await moduleDataAuthorizeSchemeBll.Update(id, entitys);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }
        #endregion
    }
}
